#include<stdio.h>
#include<iostream>
using namespace std;
typedef struct pnode
{
    int xnumber;
    int znumber;
    struct pnode *next;
}*polynomial,pnode;
void createpolynomial(polynomial &p);
void displaypolynomial(polynomial &p);
void sumpolynomial(polynomial &a,polynomial &b,polynomial &c);
void mulpolynomial(polynomial &a,polynomial &b,polynomial &c);
void sorting(polynomial &l);
void conbine(polynomial &l);
int main()
{
    polynomial a,b,c,d;
    createpolynomial(a);
    createpolynomial(b);
   // displaypolynomial(a);
    //displaypolynomial(b);
    mulpolynomial(a,b,d);
    sumpolynomial(a,b,c);
    sorting(d);
    sorting(c);
    conbine(d);
    conbine(c);
    sorting(d);
  //  sorting(c);
    displaypolynomial(d);
    displaypolynomial(c);
   return 0;
}
void createpolynomial(polynomial &p)
{
    int n,i,x,z;
    p=new pnode;
    polynomial r,s;
    cin>>n;
    r=p;
    for(i=0;i<n;i++)
    {
       s=new pnode;
       cin>>x;
       cin>>z;
       s->xnumber=x;
       s->znumber=z;
       r->next=s;
       r=s;
    }
    r->next=NULL;
}
/*void displaypolynomial(polynomial &p)
{
    polynomial q;
    q=p->next;
    while(!q)
        cout<<"0 0";
    while(q!=NULL)
    {
        if(q->xnumber==0)//&&q->next==NULL
        {
           // printf("0 0");
            break;
        }
        else
        {
           if(q->next!=NULL)
        {
        cout<<q->xnumber<<" ";
        cout<<q->znumber<<" ";
        }
        else
        {
        cout<<q->xnumber<<" ";
        cout<<q->znumber;
        }
        }


      //printf("%d ",q->xnumber);
     // printf("%d ",q->znumber);
        q=q->next;
    }
    cout<<"\n";
}*/
/*void displaypolynomial(polynomial &p)
{
    int flag=0;
    polynomial q;
    q=p->next;
    if (!q)
    {
        printf("0 0");
        //return;
    }
    while (q)
    {
       if(p->xnumber!=0)
        {
           if (!flag)
        {
            flag = 1;
        }
        else
        {
            printf(" ");
        }
        printf("%d %d", q->xnumber, q->znumber);
         q = q->next;
        }
        else if(q->xnumber==0&&p->znumber==0)
        {
              printf("0 0");
            break;
        }
        else
        {
            q=q->next;
        }

    }
    printf("\n");
}*/
void displaypolynomial(polynomial &p)
{
    int flag=0;
    polynomial q;
    q=p->next;
    if (!q)
    {
        printf("0 0");
        //return;
    }
    while (q)
    {
        if(q->znumber!=0&&q->xnumber==0)
        {
            q=q->next;
        }
        else if(q->xnumber==0&&q->znumber==0&&q->next==NULL&&flag!=0)//
            break;
      else
        {
           if (!flag)
        {
            flag = 1;
        }
        else
        {
            printf(" ");
        }
        printf("%d %d", q->xnumber, q->znumber);
         q = q->next;
        }


    }
    printf("\n");
}
void sumpolynomial(polynomial &a,polynomial &b,polynomial &c)
{
    polynomial pa,pb,r,s;
    c=new pnode;
    r=c;
    pa=a->next;
    pb=b->next;
    if(pa==NULL&&pb==NULL)
    {
       s=new pnode;
        s->xnumber=0;
        s->znumber=0;
        r->next=s;r=s;
        r->next=NULL;
    }
    if(pa==NULL)
    {
       r->next=pb;
      // r->next=NULL;
    }
    if(pb==NULL)
    {
       r->next=pa;
    //   r->next=NULL;
    }
    while(pa!=NULL&&pb!=NULL)
    {
        if(pa->znumber>pb->znumber)
        {
            s=new pnode;
            s->znumber=pa->znumber;
            s->xnumber=pa->xnumber;

            r->next=s;
            r=s;
            pa=pa->next;
        }
        if(pa->znumber<pb->znumber)
        {
             s=new pnode;
            s->znumber=pb->znumber;
            s->xnumber=pb->xnumber;

            r->next=s;
            r=s;
            pb=pb->next;
        }
        if(pa->znumber==pb->znumber)
        {
            s=new pnode;
            s->znumber=pa->znumber;
            s->xnumber=pa->xnumber+pb->xnumber;

            r->next=s;
            r=s;
            pa=pa->next;
            pb=pb->next;
        }
    }
    if(pa!=NULL)
    {
        r->next=pa;
    }
    if(pb!=NULL)
    {
        r->next=pb;
    }
}
void mulpolynomial(polynomial &a,polynomial &b,polynomial &c)
{
    polynomial pa,pb,s,r;
    c=new pnode;
    r=c;
    pa=a->next;
    pb=b->next;
     if(pa==NULL||pb==NULL)
    {
       s=new pnode;
        s->xnumber=0;
        s->znumber=0;
        r->next=s;
        r=s;
        r->next=NULL;
    }
    while(pa!=NULL)
    {
        while(pb!=NULL)
        {
        s=new pnode;
        s->xnumber=pa->xnumber*pb->xnumber;
        s->znumber=pa->znumber+pb->znumber;

        r->next=s;
        r=s;
        pb=pb->next;
        }
       pb=b->next;
       pa=pa->next;
    }

}
void sorting(polynomial &l)
{
    polynomial p,pre,q;
    p=l->next->next;
    l->next->next=NULL;
    while(p!=NULL)
    {
        q=p->next;
        pre=l;
    while(pre->next!=NULL&&pre->next->znumber>p->znumber)
    pre=pre->next;
    p->next=pre->next;
    pre->next=p;
    p=q;
    }

}

void conbine(polynomial &l)
{
    polynomial p,q,Q;
    p=l->next;
    q=p->next;
    while(p->next!=NULL)
    {
        if(q->znumber==p->znumber)
        {
            p->xnumber=p->xnumber+q->xnumber;

           // p=p->next;
         //delete(p->next);
          //  p=p->next;
          p->next=q->next;
          Q=q;
          q=q->next;
          delete Q;
        }
        else
        {
            p=p->next;
            q=q->next;
        }

    }
}

